﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Training.Library.DomainObject.Administration.Catalog;
using Training.Library.DataAccess.Administration.Catalog;
using System.Configuration;
using Training.Web.Support;
using Training.Library.DomainObject.Administration.UserManagement;

using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DataAccess.Administration.Catalog;

namespace Training.Web.Forms.Administration.Catalog
{

    public partial class PelatihanDetailForm :  BasePage
    {
        private int id = -1;

        protected override void OnLoad(EventArgs e)
        {
            try
            {

                //if (Request.Params["id"] != null)
                //{
                //    id = Convert.ToInt32(Request.Params["id"]);
                //}

                CabangRepo CabangDal = new CabangRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                KaryawanRepo KaryawanDal = new KaryawanRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                List<Karyawan> list = KaryawanDal.GetList();

                //if (base.CabangID != null)
                //{
                Cabang objCabang = CabangDal.GetById(base.CabangID);
                if (objCabang != null)
                {
                    if (!objCabang.NamaCabang.Trim().ToLower().Contains("pusat"))
                    {
                        //parameters = parameters == string.Empty ? " WHERE Pelatihan.CabangID=" + base.CabangID : parameters + " AND Pelatihan.CabangID=" + base.CabangID;
                        var query = list.Where(x => x.CabangID == base.CabangID).OrderBy(x => x.NIK);
                        list = query.ToList();
                    }
                    else
                    {
                        var query = list.OrderBy(x => x.NIK);
                        list = query.ToList();
                    }
                }
                //}




                ddlNIK.Items.Clear();

                list.ForEach(delegate(Karyawan obj)
                {
                    ddlNIK.Items.Add(new ListItem(obj.NIK + " - " + obj.NamaKaryawan, obj.NIK.ToString()));
                });

                ddlNIK.Items.Insert(0, new ListItem("-- pilih NIK --", string.Empty));

               

                //if (!Page.IsPostBack)
                //{
                //    if (id > 0) // Edit an existing item
                //    {
                //        BagianRepo BagianDal = new BagianRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                //        List<Bagian> listBagian = BagianDal.GetList();
                //        var queryBagian = listBagian.OrderBy(x => x.NamaBagian);
                //        listBagian = queryBagian.ToList();
                //        ddlBagian.Items.Clear();

                //        listBagian.ForEach(delegate(Bagian objBagian)
                //        {
                //            ddlBagian.Items.Add(new ListItem(objBagian.NamaBagian, objBagian.BagianID.ToString()));
                //        });

                //        ddlBagian.Items.Insert(0, new ListItem("-- pilih bagian --", string.Empty));


                //        PositionRepo PositionDal = new PositionRepo(ConfigurationManager.ConnectionStrings["JobdescConn"].ConnectionString);
                //        List<Position> listJabatan = PositionDal.GetList();
                //        var queryJabatan = listJabatan.OrderBy(x => x.PositionName);
                //        listJabatan = queryJabatan.ToList();
                //        ddlJabatan.Items.Clear();

                //        listJabatan.ForEach(delegate(Position objJabatan)
                //        {
                //            ddlJabatan.Items.Add(new ListItem(objJabatan.PositionName, objJabatan.PositionID.ToString()));
                //        });

                //        ddlJabatan.Items.Insert(0, new ListItem("-- pilih jabatan --", string.Empty));

                //        PesertaRepo PesertaDal = new PesertaRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                //        Peserta obj = PesertaDal.GetById(id);
                //        if (obj != null)
                //        {
                //            hfID.Value = id.ToString();
                //            txtNama.Text = obj.NamaPeserta;
                //            txtNIK.Text = obj.NIK;
                //            Position objJabatan = PositionDal.GetById(Convert.ToInt32(obj.PositionID));
                //            ddlDepartment.Items.FindByValue(objJabatan.DepartmentID.ToString()).Selected = true;
                //            ddlBagian.Items.FindByValue(obj.BagianID.ToString()).Selected = true;
                //            ddlJabatan.Items.FindByValue(obj.PositionID.ToString()).Selected = true;

                //        }
                //    }

                //    GetPrivilege();


              //  }
            }
            catch (Exception ex) {
                ClientScript.RegisterClientScriptBlock(typeof(Page), "error", "alert('" +" Error :" +ex.Message + "');", true);
                return;
            }

        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').button('disable');", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }

    
    }

}
